home *** CD-ROM | disk | FTP | other *** search
-
- Listing 9
-
- /*
- * fgetid skips leading whitespace and reads an identifier
- * from file f into string s. At most n - 1 characters
- * are copied to s, and a '\0' is appended. fgetid returns
- * the length of s, or EOF if end of file is detected.
- */
- int fgetid(FILE *f, char *s, size_t n)
- {
- char *p = s;
- int c;
-
- while (isspace(c = fgetc(f)))
- ;
- if (isalpha(c) || c == '_')
- {
- do
- if (p - s + 1 < n)
- *p++ = c;
- while (isalnum(c = fgetc(f)) || c == '_');
- ungetc(c, f);
- *p = '\0';
- return p - s;
- }
- else if (c != EOF)
- {
- ungetc(c, f);
- return 0;
- }
- else
- return EOF;
- }
-
-